package com.ymk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ymk.base.config.MsgDecrypt;
import com.ymk.base.config.ServerVerification;
import com.ymk.bo.work.DyAuthorBO;
import com.ymk.bo.work.DyMesBO;
import com.ymk.core.constants.SysConstants;
import com.ymk.service.IDyAuthorService;
import com.ymk.service.IDyMesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.Random;

@Service
public class DyService {


    @Autowired
    private IDyAuthorService dyAuthorService;

    @Autowired
    private IDyMesService dyMesService;

    @Transactional
    public void save(JSONObject json, String type) throws Exception {
        MsgDecrypt msgDecrypt = new MsgDecrypt(SysConstants.key);
        String nonce = json.getString("Nonce");
        String timeStamp = json.getString("TimeStamp");
        String encrypt = json.getString("Encrypt");
        String msgSignature = ServerVerification.getMsgSignature(SysConstants.token,timeStamp,nonce,encrypt);
        Boolean result = ServerVerification.verify(json.getString("MsgSignature"),msgSignature);

        if (result && SysConstants.AUTHOR.equals(type)){
            Map<String,String> res = msgDecrypt.decrypt(encrypt);
            JSONObject data = JSONObject.parseObject(res.get("content"));

            DyAuthorBO dyAuthorBO = new DyAuthorBO();
            dyAuthorBO.setDymes(json.toJSONString());
            dyAuthorBO.setDecryptmes(res.get("content"));
            dyAuthorBO.setDecrypAppId(res.get("appid"));
            dyAuthorBO.setEvent(data.getString("Event"));
            dyAuthorBO.setFromUserName(data.getString("FromUserName"));
            dyAuthorBO.setTicket(data.getString("Ticket"));
            dyAuthorBO.setCreateTime(data.getString("CreateTime"));
            dyAuthorBO.setMsgType(data.getString("MsgType"));

            dyAuthorService.post(dyAuthorBO);

        }
        if (result && SysConstants.MESSAGE.equals(type)){
            Map<String,String> res = msgDecrypt.decrypt(encrypt);
            JSONObject data = JSONObject.parseObject(res.get("content"));


            DyMesBO dyMesBO = new DyMesBO();
            dyMesBO.setDymes(json.toJSONString());
            dyMesBO.setDecryptmes(res.get("content"));
            dyMesBO.setDecrypAppId(res.get("appid"));


            dyMesService.post(dyMesBO);

        }

    }

    public static void main(String[] args) {
//            String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
//            Random random=new Random();
//            StringBuffer sb=new StringBuffer();
//            for(int i=0;i<43;i++){
//                int number=random.nextInt(62);
//                sb.append(str.charAt(number));
//            }
//        System.out.println(sb);
        System.out.println(SysConstants.token.length());
        System.out.println(SysConstants.key.length());

    }



}
